<?php

namespace App\Models\Notice;

use App\Helpers\Code;
use App\Models\Account\User;
use App\Models\BaseModel;

class NoticeGroup extends BaseModel
{

    protected $table = 'notice_groups';
    protected $fillable = [
        'title', 'description', 'member'
    ];
    public $sorters = [];
    public $params = ['title'];

    /**
     * 添加通知分组
     * @param $data
     * @return int
     */
    public function addGroup($data)
    {
        try {
            $userCount = User::whereIn('id', $data['member'])->count();

            if ($userCount != count($data['member'])) {
                return Code::INSIDE_ERROR;
            }
            $data['member'] = json_encode($data['member']);
            return $this->create($data);
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知分组全部
     * @return int
     */
    public function getGroupListAll()
    {
        try {
            return $this->get(['id', 'title']);
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知分组列表
     * @param $limit
     * @param $sorter
     * @param $search
     * @return int
     */
    public function getGroupList($limit, $sorter, $search)
    {
        try {
            $perPage = $limit > 0 ? $limit : $this->limit;
            $notice = $this->orderBy($sorter[0], $sorter[1]);
            $notice->select(['id', 'title', 'description', 'member', 'created_at']);

            foreach ($search as $key => $value) {
                switch ($key) {
                    case 'title':
                        $notice->where($key, 'like', '%' . $value . '%');
                        break;
                }
            }
            $model = $notice->paginate($perPage)->toArray();

            foreach ($model['data'] as &$modelValue) {
                $users = json_decode($modelValue['member'], true);
                $userNames = User::whereIn('id', $users)->get(['username'])->toArray();
                $userName = array_unique(array_flatten($userNames));
                $modelValue['member'] = implode('/', $userName);
            }

            return $model;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取通知分组详情
     * @param $id
     * @return int|null
     */
    public function getGroup($id)
    {
        try {
            $model = $this->find($id);

            if (!$model) {
               return null;
            }

            $data = $model->toArray();
            $users = json_decode($data['member'], true);
            $data['member'] = $users;
            $username = User::whereIn('id', $data['member'])->get(['username'])->toArray();
            if (count($username) > 0) {
                $userNames = array_column($username, 'username');
                $data['member_name'] = implode('/', $userNames);
            } else {
                $data['member_name'] = null;
            }

            return $data;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑通知分组
     * @param $id
     * @param $data
     * @return int|null
     */
    public function editGroup($id, $data)
    {
        try {
            $model = $this->find($id);

            if (!$model) {
                return null;
            }

            $userCount = User::whereIn('id', $data['member'])->count();

            if ($userCount != count($data['member'])) {
                return Code::INSIDE_ERROR;
            }

            $data['member'] = json_encode($data['member']);

            return $model->updateOrCreate(['id' => $id], $data);
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }
}
